<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <img src="@/assets/logo.png" alt="Logo" class="logo" />
        <h1 class="title">外卖管理系统</h1>
      </div>
      
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="用户名" 
            prefix-icon="el-icon-user"
            @keyup.enter.native="handleLogin" />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="密码" 
            prefix-icon="el-icon-lock"
            @keyup.enter.native="handleLogin" />
        </el-form-item>
        
        <el-form-item>
          <el-checkbox v-model="loginForm.rememberMe">记住我</el-checkbox>
          <a href="javascript:;" class="forget-pwd">忘记密码？</a>
        </el-form-item>
        
        <el-form-item>
          <el-button 
            :loading="loading" 
            type="primary" 
            class="login-button" 
            @click="handleLogin">
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <p>© 2023 外卖管理系统 版权所有</p>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex';

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        rememberMe: false
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ]
      },
      loading: false
    };
  },
  created() {
    // 检查是否有保存的登录信息
    const savedUsername = localStorage.getItem('username');
    if (savedUsername) {
      this.loginForm.username = savedUsername;
      this.loginForm.rememberMe = true;
    }
  },
  methods: {
    ...mapActions(['login']),
    
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (!valid) return;
        
        this.loading = true;
        
        // 保存用户名到本地存储
        if (this.loginForm.rememberMe) {
          localStorage.setItem('username', this.loginForm.username);
        } else {
          localStorage.removeItem('username');
        }
        
        // 调用登录接口
        this.login(this.loginForm)
          .then(() => {
            this.$message.success('登录成功');
            this.$router.push({ path: '/dashboard' });
          })
          .catch(error => {
            this.$message.error(error.message || '登录失败，请重试');
          })
          .finally(() => {
            this.loading = false;
          });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
  
  .login-box {
    width: 400px;
    padding: 40px;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    
    .login-header {
      text-align: center;
      margin-bottom: 40px;
      
      .logo {
        width: 80px;
        height: 80px;
        margin-bottom: 16px;
      }
      
      .title {
        font-size: 24px;
        color: #303133;
        margin: 0;
      }
    }
    
    .login-form {
      .forget-pwd {
        float: right;
        color: #409eff;
        font-size: 14px;
        text-decoration: none;
        
        &:hover {
          text-decoration: underline;
        }
      }
      
      .login-button {
        width: 100%;
      }
    }
    
    .login-footer {
      margin-top: 40px;
      text-align: center;
      color: #909399;
      font-size: 14px;
    }
  }
}

// 移动端适配
@media screen and (max-width: 576px) {
  .login-container {
    .login-box {
      width: 90%;
      padding: 30px 20px;
    }
  }
}
</style> 